package com.tcl.aota.manage.imp;

import com.alibaba.fastjson.JSON;
import com.tcl.aota.cache.AppCacheManage;
import com.tcl.aota.common.constants.Constants;
import com.tcl.aota.common.utils.StringUtil;
import com.tcl.aota.manage.CurefManage;
import com.tcl.aota.persistent.dao.db.AppCurefDao;
import com.tcl.aota.persistent.dao.db.AppDAO;
import com.tcl.aota.persistent.dao.db.CurefDao;
import com.tcl.aota.persistent.model.AppCuref;
import com.tcl.aota.persistent.model.Curef;
import com.tcl.aota.vo.Product;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Curef Service
 *
 * @author kelong
 * @date 5/7/15
 */
@Service("curefManage")
public class CurefManageImpl implements CurefManage {

    private final static Logger LOG = Logger.getLogger(CurefManageImpl.class);

    @Resource
    private CurefDao curefDao;

    @Resource
    private AppCurefDao appCurefDao;

    @Resource
    private AppDAO appDAO;

    @Resource
    private AppCacheManage appCacheManage;

    @Override
    public List<Product> selectProduct(String product) {
        List<Product> products = new ArrayList<Product>();
        try {
            Map<String, Object> param = new HashMap<>();
            if (!StringUtils.isEmpty(product)) {
                param.put("product", product);
            }
            List<Curef> curefs = curefDao.selectProduct(param);
            if (curefs != null) {
                for (Curef curef : curefs) {
                    Product pdt = new Product();
                    pdt.setProduct(curef.getProduct());
                    products.add(pdt);
                }
            }
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }
        return products;
    }

    @Override
    public List<Curef> selectCurefByProducts(List<String> products) {
        List<Curef> curefs = null;
        try {
            curefs = curefDao.selectCurefByProducts(products);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return curefs;
    }


    @Override
    public List<AppCuref> selectCurefByAppId(Long appId) {
        List<AppCuref> curefs = null;
        try {
            curefs = appCurefDao.selectCurefByAppId(appId);
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }
        return curefs;
    }

    @Override
    public List<Curef> selectCurefByApp(Long appId) {
        List<Curef> curefs = null;
        try {
            curefs = curefDao.selectCurefByApp(appId);
        } catch (Exception e) {
            LOG.error(e.getMessage(),e);
        }
        return curefs;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateCuref(List<Curef> curefList, List<String> createUsers) throws Exception {
        curefDao.deleteAll();
        int num = curefDao.batchInsert(curefList);
        if (createUsers != null && createUsers.size() > 0) {
            curefDao.updateFlagByCreateUser(createUsers);
        }
        return num;
    }

    @Override
    public List<Curef> selectAllCuref(int flag) {
        String cuKey = StringUtil.append(Constants.REDIS.CUREF, flag);
        List<Curef> curefs = appCacheManage.getCurefCache(cuKey);
        if (StringUtil.isNullList(curefs)) {
            synchronized (this) {
                curefs = appCacheManage.getCurefCache(cuKey);
                if (StringUtil.isNullList(curefs)) {
                    curefs = curefDao.selectAllCuref(flag);
                    if (!StringUtil.isNullList(curefs)){
                        appCacheManage.set(cuKey, JSON.toJSONString(curefs));
                    }
                }
            }
        }
        return curefs;
    }

    @Override
    public int updateFlagByCreateUser(List<String> list) {
        return curefDao.updateFlagByCreateUser(list);
    }
}
